//
//  YXYGroupHotCollectionViewCell.m
//  Topit
//
//  Created by dllo on 15/12/11.
//  Copyright © 2015年 YXY. All rights reserved.
//

#import "YXYGroupHotCollectionViewCell.h"
#import "YXYGroupHotsixTableViewCell.h"
#import "YXYGroupHotNineTableViewCell.h"
#import "YXYGroupHotOneTableViewCell.h"
@implementation YXYGroupHotCollectionViewCell

- (void)dealloc {
    [_num release];
    [_tableView release];
    [_dataSource release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[YXYGroupHotsixTableViewCell class] forCellReuseIdentifier:@"YXYGroupHotsixTableViewCellIdentifier"];
        [_tableView registerClass:[YXYGroupHotNineTableViewCell class] forCellReuseIdentifier:@"YXYGroupHotNineTableViewCellIdentifier"];
        [_tableView registerClass:[YXYGroupHotOneTableViewCell class] forCellReuseIdentifier:@"YXYGroupHotOneTableViewCellIdentifier"];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"table"];
        [self addSubview:_tableView];
        [_tableView release];
    }
    return self;
}

-(void)setDataSource:(NSMutableArray *)dataSource {
    if (_dataSource != dataSource) {
        [_dataSource release];
        _dataSource = [dataSource retain];
    }
}
- (void)setNum:(NSDictionary *)num {
    if (_num != num) {
        [_num release];
        _num = [num retain];
    }
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    _tableView.frame = CGRectMake(0, 0, self.width, self.height);
}

#pragma mark - tableviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [user objectForKey:@"NightMode"];
    
    YXYGroupHotModel *groupHotModel = _dataSource[indexPath.row];
    if (groupHotModel.items.count > 1 && groupHotModel.items.count < 7) {
        YXYGroupHotsixTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXYGroupHotsixTableViewCellIdentifier"];
        [cell setGroupHotModel:groupHotModel];
        if ([passWord isEqualToString:@"Night"]) {
            cell.backgroundColor = [UIColor colorWithRed:34 / 255.0 green:34 / 255.0 blue:34 / 255.0 alpha:1];
        }else {
            cell.backgroundColor = [UIColor whiteColor];
        }
        [cell.collectionView reloadData];
        return cell;
    }else if (groupHotModel.items.count == 9) {
        YXYGroupHotNineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXYGroupHotNineTableViewCellIdentifier"];
        [cell setGroupHotModel:groupHotModel];
        if ([passWord isEqualToString:@"Night"]) {
            cell.backgroundColor = [UIColor colorWithRed:34 / 255.0 green:34 / 255.0 blue:34 / 255.0 alpha:1];
        }else {
            cell.backgroundColor = [UIColor whiteColor];
        }
        [cell.collectionView reloadData];
        return cell;
    }else {
        YXYGroupHotOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXYGroupHotOneTableViewCellIdentifier"];
        [cell setGroupHotModel:groupHotModel];
        if ([passWord isEqualToString:@"Night"]) {
            cell.backgroundColor = [UIColor colorWithRed:34 / 255.0 green:34 / 255.0 blue:34 / 255.0 alpha:1];
        }else {
            cell.backgroundColor = [UIColor whiteColor];
        }
        [cell.collectionView reloadData];
        return cell;
    }
    
}



- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel *num = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, self.width, 10)];
    num.font = [UIFont systemFontOfSize:10];
    num.textColor = [UIColor grayColor];
    num.text = [NSString stringWithFormat:@"%@ %@", _num[@"num"], @"小组"];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 20)];
    [view addSubview:num];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 330;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

#pragma mark - 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YXYGroupHotModel *groupHotModel = _dataSource[indexPath.row];
  
}

#pragma mark - 底部刷新
- (void)getFootRefresh {
    NSString *num = [NSString stringWithFormat:@"%d", _number];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:num, @"offset", nil];
    [YXYNetWorkTool getWithUrl:kGroupHotURL parameter:dic httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        self.num = result[@"info"];
        for (NSDictionary *dic in result[@"item"]) {
            YXYGroupHotModel *groupHomePageModel = [[YXYGroupHotModel alloc] initWithDictionary:dic];
            [_dataSource addObject:groupHomePageModel];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView reloadData];
        });
    } fail:^(NSError *error) {
        
    }];
    
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint offset = scrollView.contentOffset;
    CGRect bounds = scrollView.bounds;
    CGSize size = scrollView.contentSize;
    UIEdgeInsets inset = scrollView.contentInset;
    CGFloat currentOffset = offset.y + bounds.size.height - inset.bottom;
    CGFloat maximumOffset = size.height;
    
    if ((int)currentOffset == (int)maximumOffset) {
        _number += 30;
        [self getFootRefresh];
    }else {
        
    }
    if (currentOffset == 14980) {
        UIAlertView *allert = [[UIAlertView alloc] initWithTitle:@"别滑了" message:@"屁股疼" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        [allert show];
        
    }
}
@end
